﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/13 20:06:16
*
* */
namespace Novel.Sys.Dal
{
    public class novel_user_info
    {
        //public Model.novel_user_info GetModel(int uid)
        //{
        //    Model.novel_user_info model = new Model.novel_user_info();
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select * from [novel_user_info] where uid=@uid");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@uid", SqlDbType.Int)};
        //    parameters[0].Value = uid;
        //    DataSet ds = DbHelpers.GetDataSet(strSql.ToString(), parameters);
        //    if (ds.Tables[0].Rows.Count > 0)
        //    {
        //        if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
        //        {
        //            model.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
        //        }
        //        if (ds.Tables[0].Rows[0]["imei"] != null && ds.Tables[0].Rows[0]["imei"].ToString() != "")
        //        {
        //            model.Imei = ds.Tables[0].Rows[0]["imei"].ToString();
        //        }
        //        else model.Imei = "";
        //        if (ds.Tables[0].Rows[0]["signature"] != null && ds.Tables[0].Rows[0]["signature"].ToString() != "")
        //        {
        //            model.Signature = ds.Tables[0].Rows[0]["signature"].ToString();
        //        }
        //        else model.Signature = "";

        //        if (ds.Tables[0].Rows[0]["money"] != null && ds.Tables[0].Rows[0]["money"].ToString() != "")
        //        {
        //            model.Money = float.Parse(ds.Tables[0].Rows[0]["money"].ToString());
        //        }
        //        else model.Money = 0.0f;
        //        if (ds.Tables[0].Rows[0]["gold"] != null && ds.Tables[0].Rows[0]["gold"].ToString() != "")
        //        {
        //            model.Gold = float.Parse(ds.Tables[0].Rows[0]["gold"].ToString());
        //        }
        //        else model.Gold = 0.0f;
        //        return model;
        //    }
        //    else
        //        return null;
        //}

        /// <summary>
        /// 获取任意条件一个表键数据
        /// </summary>
        /// <param name="strWhere"> 如 UID=1000 AND id=1000</param>
        /// <param name="keys">如 表1只能获取一个</param>
        /// <returns></returns>
        public string GetWhere(string strWhere, string keys)
        {
            string sql = "select top 1  " + keys + "   from [novel_user_info] where " + strWhere;
            DataSet ds = DbHelpers.GetDataSet(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0][keys.Trim()] != null && ds.Tables[0].Rows[0][keys.Trim()].ToString() != "")
                {
                    return ds.Tables[0].Rows[0][keys.Trim()].ToString();
                }
                else
                    return "";
            }
            else return "";
        }

        /// <summary>
        /// 根据条件更新任意一个键数据
        /// </summary>
        /// <param name="StrWhere">如:[ID]='1'</param>
        /// <param name="Keys"> 如:[money]='2.3'</param>
        /// <returns></returns>
        public bool UpdateWhere(string StrWhere, string Keys)
        {
            string sql = "UPDATE TOP(1) [novel_user_info] SET "+ Keys+" WHERE ("+StrWhere+")";
            if (DbHelpers.NonQuery(sql) > 0)
                return true;
            else
                return false;
        }





        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.novel_user_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [novel_user_info] ([uid], [imei], [money], [gold], [signature]) VALUES (");
            strSql.Append("@uid,@imei,@money,@gold,@signature");
            strSql.Append(");select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int),
                    new SqlParameter("@imei", SqlDbType.NVarChar),
                    new SqlParameter("@money", SqlDbType.Float),
                    new SqlParameter("@gold", SqlDbType.Float),
                    new SqlParameter("@signature", SqlDbType.NVarChar)};
            parameters[0].Value = model.Uid;
            parameters[1].Value = model.Imei;
            parameters[2].Value = model.Money;
            parameters[3].Value = model.Gold;
            parameters[4].Value = model.Signature;

            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 验证IMEI
        /// </summary>
        /// <param name="IMEI"></param>
        /// <returns></returns>
        public bool Exists(string IMEI)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_user_info]");
            strSql.Append(" where imei=@imei");
            SqlParameter[] parameters = {
                    new SqlParameter("@imei", SqlDbType.NVarChar)};
            parameters[0].Value = IMEI;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 加减金币
        /// </summary>
        /// <param name="Uid"></param>
        /// <param name="money"></param>
        /// <param name="b">true 减法,false 加法</param>
        /// <returns></returns>
        public bool UpdateMoney(int Uid, float money,bool b)
        {
            string StrMoney = GetWhere("[uid]='" + Uid + "' ", " money ");
            if (!string.IsNullOrEmpty(StrMoney))
            {
                if (b)//减
                {

                        float tem = float.Parse(StrMoney) - money;
                        return UpdateWhere("[uid]='" + Uid + "'", "[money]='" + tem + "'");

                }
                else//加
                {
                    float tem = float.Parse(StrMoney) + money;
                    return UpdateWhere("[uid]='" + Uid + "'", "[money]='" + tem + "'");
                }
            }
            else return false;
        }


        /// <summary>
        /// 获取用户全部数据 包括 user user_info session
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public Model.novel_user_info GetModel(int uid)
        {
            Model.novel_user_info info = new Model.novel_user_info();
            Model.novel_user User = new Model.novel_user();
            Model.novel_session Session = new Model.novel_session();
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from novel_user,novel_session,novel_user_info where  ")
                .Append("novel_user.id=novel_session.uid And novel_user.id=novel_user_info.uid  and  novel_user.id=").Append(uid);
            DataSet ds = DbHelpers.GetDataSet(sb.ToString().ToLower());
            if (ds.Tables[0].Rows.Count > 0)
            {

                #region Novel_Users
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    User.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["gid"] != null && ds.Tables[0].Rows[0]["gid"].ToString() != "")
                {
                    User.Gid = int.Parse(ds.Tables[0].Rows[0]["gid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    User.Name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nick"] != null && ds.Tables[0].Rows[0]["nick"].ToString() != "")
                {
                    User.Nick = ds.Tables[0].Rows[0]["nick"].ToString();
                }
                if (ds.Tables[0].Rows[0]["passwd"] != null && ds.Tables[0].Rows[0]["passwd"].ToString() != "")
                {
                    User.Passwd = ds.Tables[0].Rows[0]["passwd"].ToString();
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    User.Email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["phone"] != null && ds.Tables[0].Rows[0]["phone"].ToString() != "")
                {
                    User.Phone = ds.Tables[0].Rows[0]["phone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["avatar"] != null && ds.Tables[0].Rows[0]["avatar"].ToString() != "")
                {
                    User.Avatar = ds.Tables[0].Rows[0]["avatar"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sex"] != null && ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    User.Sex = int.Parse(ds.Tables[0].Rows[0]["sex"].ToString());
                }
                #endregion


                #region Novel_Session
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    Session.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["session"] != null && ds.Tables[0].Rows[0]["session"].ToString() != "")
                {
                    Session.Session = ds.Tables[0].Rows[0]["session"].ToString();
                }
                if (ds.Tables[0].Rows[0]["created"] != null && ds.Tables[0].Rows[0]["created"].ToString() != "")
                {
                    Session.Created = DateTime.Parse(ds.Tables[0].Rows[0]["created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endtime"] != null && ds.Tables[0].Rows[0]["endtime"].ToString() != "")
                {
                    Session.Endtime = DateTime.Parse(ds.Tables[0].Rows[0]["endtime"].ToString());
                }
                #endregion


                #region Novel_User_Info
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    info.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["imei"] != null && ds.Tables[0].Rows[0]["imei"].ToString() != "")
                {
                    info.Imei = ds.Tables[0].Rows[0]["imei"].ToString();
                }
                if (ds.Tables[0].Rows[0]["money"] != null && ds.Tables[0].Rows[0]["money"].ToString() != "")
                {
                    info.Money = float.Parse(ds.Tables[0].Rows[0]["money"].ToString());
                }
                if (ds.Tables[0].Rows[0]["gold"] != null && ds.Tables[0].Rows[0]["gold"].ToString() != "")
                {
                    info.Gold = float.Parse(ds.Tables[0].Rows[0]["gold"].ToString());
                }
                if (ds.Tables[0].Rows[0]["signature"] != null && ds.Tables[0].Rows[0]["signature"].ToString() != "")
                {
                    info.Signature = ds.Tables[0].Rows[0]["signature"].ToString();
                }
                #endregion

                info.User = User;
                info.Session = Session;



                return info;
            }
            else
                return info;
        }

        public Model.novel_user_info GetModel(int uid,string session)
        {
            Model.novel_user_info info = new Model.novel_user_info();
            Model.novel_user User = new Model.novel_user();
            Model.novel_session Session = new Model.novel_session();
            StringBuilder sb = new StringBuilder();
            sb.Append("  select * from novel_user,novel_session,novel_user_info where  novel_user.id=novel_session.uid And novel_user.id=novel_user_info.uid  and  novel_user.id='");
            sb.Append(uid).Append("' and novel_session.session='").Append(session).Append("'");
            DataSet ds = DbHelpers.GetDataSet(sb.ToString().ToLower());
            if (ds.Tables[0].Rows.Count > 0)
            {

                #region Novel_Users
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    User.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["gid"] != null && ds.Tables[0].Rows[0]["gid"].ToString() != "")
                {
                    User.Gid = int.Parse(ds.Tables[0].Rows[0]["gid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    User.Name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nick"] != null && ds.Tables[0].Rows[0]["nick"].ToString() != "")
                {
                    User.Nick = ds.Tables[0].Rows[0]["nick"].ToString();
                }
                if (ds.Tables[0].Rows[0]["passwd"] != null && ds.Tables[0].Rows[0]["passwd"].ToString() != "")
                {
                    User.Passwd = ds.Tables[0].Rows[0]["passwd"].ToString();
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    User.Email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["phone"] != null && ds.Tables[0].Rows[0]["phone"].ToString() != "")
                {
                    User.Phone = ds.Tables[0].Rows[0]["phone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["avatar"] != null && ds.Tables[0].Rows[0]["avatar"].ToString() != "")
                {
                    User.Avatar = ds.Tables[0].Rows[0]["avatar"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sex"] != null && ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    User.Sex = int.Parse(ds.Tables[0].Rows[0]["sex"].ToString());
                }
                #endregion


                #region Novel_Session
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    Session.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["session"] != null && ds.Tables[0].Rows[0]["session"].ToString() != "")
                {
                    Session.Session = ds.Tables[0].Rows[0]["session"].ToString();
                }
                if (ds.Tables[0].Rows[0]["created"] != null && ds.Tables[0].Rows[0]["created"].ToString() != "")
                {
                    Session.Created = DateTime.Parse(ds.Tables[0].Rows[0]["created"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endtime"] != null && ds.Tables[0].Rows[0]["endtime"].ToString() != "")
                {
                    Session.Endtime = DateTime.Parse(ds.Tables[0].Rows[0]["endtime"].ToString());
                }
                #endregion


                #region Novel_User_Info
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    info.Uid = int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["imei"] != null && ds.Tables[0].Rows[0]["imei"].ToString() != "")
                {
                    info.Imei = ds.Tables[0].Rows[0]["imei"].ToString();
                }
                if (ds.Tables[0].Rows[0]["money"] != null && ds.Tables[0].Rows[0]["money"].ToString() != "")
                {
                    info.Money = float.Parse(ds.Tables[0].Rows[0]["money"].ToString());
                }
                if (ds.Tables[0].Rows[0]["gold"] != null && ds.Tables[0].Rows[0]["gold"].ToString() != "")
                {
                    info.Gold = float.Parse(ds.Tables[0].Rows[0]["gold"].ToString());
                }
                if (ds.Tables[0].Rows[0]["signature"] != null && ds.Tables[0].Rows[0]["signature"].ToString() != "")
                {
                    info.Signature = ds.Tables[0].Rows[0]["signature"].ToString();
                }
                #endregion

                info.User = User;
                info.Session = Session;



                return info;
            }
            else
                return info;
        }

        /// <summary>
        /// 根据imei获取uid
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        public int ImeiGetUid(string imei)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uid from [novel_user_info] where imei='" + imei + "'");
            DataSet ds = DbHelpers.GetDataSet(strSql.ToString().ToLower());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["uid"] != null && ds.Tables[0].Rows[0]["uid"].ToString() != "")
                {
                    return int.Parse(ds.Tables[0].Rows[0]["uid"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }





    }
}
